package com.cy.store.controller;

import com.cy.store.service.ICartService;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author: AngJie
 * @create: 2022-02-10 15:12
 **/
@RestController
@RequestMapping("carts")
public class CartController extends BaseController {

    @Autowired
    ICartService cartService;

    @RequestMapping("add_to_cart")
    public JsonResult addToCart(HttpSession session, Integer pid, Integer amount) {
        cartService.addToCart(getUidFromSession(session),
                pid,
                amount,
                getUsernameFromSession(session));

        return new JsonResult(OK);
    }

    @RequestMapping({"/", ""})
    public JsonResult<List<CartVo>> getVo(HttpSession session) {
        List<CartVo> data = cartService.getVoByUid(getUidFromSession(session));
        return new JsonResult(OK, data);
    }

    @RequestMapping("num/{operation}/{cid}")
    public JsonResult<Integer> addNum(HttpSession session, @PathVariable Integer cid,@PathVariable String operation) {
        Integer uid = getUidFromSession(session);
        String username = getUsernameFromSession(session);
        Integer data = cartService.addNum(cid, username, uid,operation);
        return new JsonResult(OK, data);
    }

    @RequestMapping("list")
    public JsonResult<List<CartVo>> getVoByCidList(HttpSession session, @RequestParam("cids") List<Integer> cids) {
        Integer uid = getUidFromSession(session);
        List<CartVo> data = cartService.getVoByCid(cids, uid);
        return new JsonResult(OK, data);
    }
}
